<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>拖拽滑动效果</title>
    <style>
        *{
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        html, body, #outer{
            width: 100%;
            height: 100%;
            overflow: hidden;
            position: relative;
        }

        #inner{
            width: 150px;
            height: 200px;
            background: #f6c;
            position: absolute;
        }
    </style>
</head>
<body>
    <div id="outer">
        <div id="inner"></div>
    </div>

    <script>
        //获取 outer 元素
        let outer = document.querySelector("#outer");
        let inner = document.querySelector("#inner");

        //inner绑定事件
        inner.ontouchstart = function(e){
            //移除过渡
            this.style.transition = 'none';
            //获取内部元素距离左侧的偏移
            this.l = inner.offsetLeft;
            //获取按下时, 手指距离左侧的偏移
            this.x = e.changedTouches[0].clientX;

            this.t = inner.offsetTop;
            this.y = e.changedTouches[0].clientY;
        }

        inner.ontouchmove = function(e){
            let _x = e.changedTouches[0].clientX;
            //计算
            let newLeft = _x - (this.x - this.l);
            //判断
            if(newLeft <= 0){
                newLeft = 0;
            }

            let _y = e.changedTouches[0].clientY;
            let newTop = _y - (this.y - this.t);

            //设置
            inner.style.left = newLeft + 'px';
            inner.style.top = newTop + 'px';
        }

        inner.ontouchend = function(){
            this.style.transition = 'all 0.5s';

            //判断当前的 left 值
            //获取当前的 left 的值
            let left = this.offsetLeft;
            //获得中间值
            let middle = (outer.offsetWidth - inner.offsetWidth) / 2;
            //判断
            if(left < middle){
                inner.style.left = '0px';
            }else{
                let maxLeft = outer.offsetWidth - inner.offsetWidth;
                inner.style.left = maxLeft + 'px';
            }
            
        }
    </script>
</body>
</html>